<template>
  <div class="content">
    <div class="common-seach-wrap">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="信息类型">
          <el-select v-model="module_type" placeholder="请选择类型" clearable style="width: 200px">
            <el-option
              :label="v.type_name"
              :value="v.type_key"
              v-for="v in types"
              :key="v.type_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getTableInfo">查询</el-button>
          <el-button type="success" @click="handleType">管理信息类型</el-button>
          <el-button type="primary" @click="handleOpen('add')">添加信息</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" row-key="module_id" height="100%" stripe :border="true">
      <el-table-column prop="module_id" label="ID" fixed="left" width="50"></el-table-column>
      <el-table-column prop="type_name" label="信息类型" width="200"></el-table-column>
      <el-table-column prop="title" label="标题" width="300"></el-table-column>
      <el-table-column prop="content" label="内容摘要">
        <template #default="scope">
          <div v-html="scope.row.content" class="content-preview"></div>
        </template>
      </el-table-column>
      <el-table-column prop="display_order" label="排序" width="100"></el-table-column>
      <el-table-column prop="" label="图片" width="150">
        <template #default="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="scope.row.image_url"
            :zoom-rate="1.2"
            :max-scale="7"
            :min-scale="0.2"
            :preview-src-list="[scope.row.image_url]"
            show-progress
            :initial-index="4"
            fit="cover"
            :preview-teleported="true"
          />
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" width="300"> </el-table-column>
      <el-table-column label="操作" fixed="right" align="center">
        <template #default="scope">
          <el-button type="primary" size="small" @click="handleOpen('edit', scope.row)">
            编辑
          </el-button>
          <el-button type="danger" size="small" @click="delInfo(scope.row.module_id)">
            删除
          </el-button>
          <el-button type="success" size="small" @click="handleView(scope.row)"> 预览 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-divider border-style="none" />
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      background
      :current-page="curPage"
      :page-size="pageSize"
      layout="total, sizes,prev, pager, next, jumper"
      :total="total"
    ></el-pagination> -->
    <Dialog ref="infoDrawer" @doneSave="getTableInfo" />
    <PreView ref="preDrawer" />
    <TypeVue ref="typeDrawer" @done="getTypes" />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, defineEmits, reactive, watch, provide, computed } from "vue";
import Dialog from "./component/dialogForm.vue";
import PreView from "./component/preview.vue";
import TypeVue from "./component/type.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { getTypeListAPi, getListAPi, delModuleApi } from "@/api/carfilm/companymodule";
const total = ref(0);
const curPage = ref(1);
const pageSize = ref(20);
const tableData = ref([]);
const infoDrawer = ref<any>(null);
const preDrawer = ref<any>(null);
const typeDrawer = ref<any>(null);
const types = ref<any[]>([]);
provide("types", types);
const module_type = ref("");
onMounted(() => {
  getTypes();
  getTableInfo();
});
const handleSizeChange = (val: number) => {
  pageSize.value = val;
  getTableInfo();
};
const handleCurrentChange = (val: number) => {
  curPage.value = val;
  getTableInfo();
};
const getTypes = () => {
  getTypeListAPi().then((res: any) => {
    types.value = res.data;
  });
};
function getTableInfo() {
  getListAPi({
    module_type: module_type.value,
  }).then((res: any) => {
    tableData.value = res.data;
  });
}
function delInfo(id: number) {
  ElMessageBox.confirm("确认要删除该条记录吗?", "警告！", {
    confirmButtonText: "确认删除",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delModuleApi(id).then((res: any) => {
        getTableInfo();
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除已取消",
      });
    });
}

// 打开添加数据窗口
const emits = defineEmits(["openForm", "search"]);
function handleOpen(open_type: string, row?: any) {
  infoDrawer.value.open(open_type, row);
}
function handleType() {
  typeDrawer.value.open();
}
function handleView(row: any) {
  preDrawer.value.open(row);
}
defineExpose({
  getTableInfo,
});
</script>

<style lang="scss">
.common-seach-wrap {
  display: flex;
  align-items: center;
  justify-content: space-between;
  .el-input {
    width: 300px;
  }
}
.el-table {
  margin-top: 10px;
}
.content-preview {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
